/*
 * $RCSfile: TimersManage.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-11-27  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import com.i366.cache.Cache;
import com.i366.room.model.RoomPersion;
import com.i366.room.model.impl.Room;

/**
 * <p>Title: TimersManage</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class TimersManage {
	public static Logger  logger = Logger.getLogger(TimersManage.class);
	public static void init() {
		TimersManage tm = new TimersManage();
		tm.roomThreadTrim();
		tm.GongGaoTrim();
	}
	
	
	public void GongGaoTrim() {
		Timer tim = new Timer();
		TimerTask tt = new TimerTask() {
			public void run() {
				try {
					TsUtil.sendGongGaoAll();
				} catch (Exception e) {
					logger.error("", e);
				}
				
			}
		};
		tim.schedule(tt,0,1000 * 60 * 2);
	}
	
    public void roomThreadTrim() {
    	Timer tim = new Timer();
    	TimerTask tt = new TimerTask() {
			public void run() {
				Object[] objs;
				synchronized (Cache.getRoomThreads()) {
					objs = Cache.getRoomThreads().values().toArray();
				}
				for (Object obj : objs) {
					if (obj != null) {
						RoomThread roomThread = (RoomThread) obj;
						//���߳�
						if ((System.currentTimeMillis() - roomThread.getOnlineTime()) > 30 * 1000) {
							logger.error("1==================================================type:" + roomThread.getClose());
							Room room = roomThread.getRoom();
							int roomId = room.getRoomId();
							try {
								logger.debug("roomId/roomStatus/getBisaiStatus:" + room.getRoomId() + "/" + room.getRoomStatus() + "/" + room.getBisaiStatus());
								logger.debug("room audMap:");
								try {
								Object[] audMapObjects = roomThread.getRoom().getAudMap().toArray();
								for (Object audObject : audMapObjects) {
									if (audObject == null) {
										continue;
									}
									RoomPersion rp = (RoomPersion) audObject;
									logger.debug("userId/chip/nowChip/type/status:" + rp.getUserId() + "/" + rp.getChouma() + "/" + rp.getNowcounma() + "/" + rp.getType() + "/" + rp.getStatus());
								}
								logger.debug("roomPersions :");
								for (RoomPersion rp  : roomThread.getRoom().getRoomPersions()) {
									if (rp == null) {
										continue;
									}
									logger.debug("userId/chip/nowChip/type/status:" + rp.getUserId() + "/" + rp.getChouma() + "/" + rp.getNowcounma() + "/" + rp.getType() + "/" + rp.getStatus());
								}
								}catch (Exception e1) {
									logger.error(e1, e1);
								}
//								room.getRobotManage().recoverAllRobot(room.getRoomId());
								room.getRobotManage().closeRoom();
								Cache.getRoomThreads().remove(roomId);
							} catch (Exception e) {
								logger.error("roomThreadTrim err + roomid:" + roomId, e);
							}
							logger.error("2==================================================");
						}
					}
				}
			}
		};
    	tim.schedule(tt,0,1000 * 60 * 10);
    }
}

